<template>
  <div>
    <!-- 登录状态 -->
    <van-cell-group class="user_info" v-if="user">
      <van-cell center class="user_content" :border="false">
        <van-image
          slot="icon"
          width="70"
          height="70"
          round
          fit="cover"
          :src="currentUser.photo"
        />
        <div slot="title">{{ currentUser.name }}</div>
        <van-button size="small" round to="/userAlter">编辑资料</van-button>
      </van-cell>
      <van-grid :border="false">
        <van-grid-item>
          <div slot="text" class="user_data">
            <div slot="icon">头条</div>
            <div slot="text">{{ currentUser.art_count }}</div>
          </div>
        </van-grid-item>
        <van-grid-item>
          <div slot="text" class="user_data">
            <div slot="icon">关注</div>
            <div slot="text">{{ currentUser.follow_count }}</div>
          </div>
        </van-grid-item>
        <van-grid-item>
          <div slot="text" class="user_data">
            <div slot="icon">粉丝</div>
            <div slot="text">{{ currentUser.fans_count }}</div>
          </div>
        </van-grid-item>
        <van-grid-item>
          <div slot="text" class="user_data">
            <div slot="icon">获赞</div>
            <div slot="text">{{ currentUser.like_count }}</div>
          </div>
        </van-grid-item>
      </van-grid>
    </van-cell-group>
    <!-- 为登录状态 -->
    <div class="not_login" @click="handleToLogin" v-else>
      <div class="login_img">
        <img class="img" src="~@/assets/images/mobile.png" alt="" srcset="" />
      </div>
      <div class="login_text">登录 / 注册</div>
    </div>
    <van-grid :column-num="2">
      <van-grid-item icon="star-o" text="收藏" />
      <van-grid-item icon="like-o" text="历史" />
    </van-grid>
    <van-cell title="消息通知" class="t_c" is-link></van-cell>
    <van-cell title="小智同学" is-link to="/chat"></van-cell>

    <van-cell
      title="退出登录"
      v-if="user"
      class="user_btn t_c"
      @click="handleOutLogin"
    ></van-cell>
  </div>
</template>

<script>
import store from "@/store";
import { mapState } from "vuex";
import { getCurrentUser } from "@/request/user";

export default {
  data() {
    return {
      currentUser: {}
    };
  },
  // 计算属性 值改变了 才会触发事件
  computed: {
    ...mapState(["user"])
  },
  created() {
    this._getCurrentUser();
  },
  methods: {
    async _getCurrentUser() {
      const { data } = await getCurrentUser();
      this.currentUser = data.data;
    },
    // 登录跳转
    handleToLogin() {
      this.$router.push("/login");
    },
    // 退出登录
    handleOutLogin() {
      this.$dialog
        .confirm({
          title: "提示",
          message: "是否退出登录"
        })
        .then(() => {
          this.$store.commit("setUser", null);
        })
        .catch(() => {
          // on cancel
        });
    }
  }
};
</script>

<style  lang="less">
.user_info {
  background: url("~@/assets/images/banner.png");
  .user_content {
    background-color: unset;
  }
  /deep/ .van-grid-item__content {
    background-color: unset;
  }
}

.user_data {
  font-size: 14px;
  text-align: center;
  color: white;
}
.user_btn {
  text-align: center;
  color: #d86262;
}
.not_login {
  width: 100vw;
  height: 180px;
  background: url("~@/assets/images/banner.png");
  .login_img {
    margin: 0 auto;
    line-height: 180px;
    width: 66px;
    height: 66px;
    .img {
      width: 100%;
      height: 100%;
    }
  }
  .login_text {
    line-height: 100px;
    text-align: center;
    font-size: 16px;
    color: white;
  }
}
</style>